﻿// See https://aka.ms/new-console-template for more information
using Microsoft.Extensions.DependencyInjection;
using Workflow2211A.Consol.Data;
using Workflow2211A.Consol.Workflows;
using WorkflowCore.Interface;

//Console.WriteLine("Hello, World!");

//IServiceProVider 服务提供者
var serviceProVider = ConfigureServices();
//获取工作流主机
var host = serviceProVider.GetService<IWorkflowHost>();
//启动工作流主机
host.Start();
//注册工作流
host.RegisterWorkflow<HelloWorldWorkflow>();
host.RegisterWorkflow<OutcomeWorkflow>();

host.RegisterWorkflow<AddNumberWorkflow, MyDataClass>();
host.RegisterWorkflow<LoopWorkflow, LoopData>();

host.RegisterWorkflow<WaitForWorkflow>();

host.RegisterWorkflow<MoreResultWorkflow>();
host.RegisterWorkflow<ForeachWorkflow>();
host.RegisterWorkflow<WhileWorkflow,LoopData>();
host.RegisterWorkflow<IfWorkflow,LoopData>();

//发起流程
//var workflowId= await host.StartWorkflow(nameof(HelloWorldWorkflow), 1);
//var workflowId= await host.StartWorkflow(nameof(OutcomeWorkflow), 1);

//var data = new MyDataClass() { Input1 = 1, Input2 = 2 };
//var workflowId = await host.StartWorkflow(nameof(AddNumberWorkflow), 1, data);
//Thread.Sleep(1000);//休眠1秒，等待工作流执行完成
//Console.WriteLine("data的返回结果是：" + data.OutputResult);


//var workflowId = await host.StartWorkflow(nameof(LoopWorkflow), 1,new LoopData());
//Console.WriteLine("发起成功，流程实例ID：" + workflowId);


//var workflowId = await host.StartWorkflow(nameof(WaitForWorkflow), 1);
//Console.WriteLine("发起成功，流程实例ID：" + workflowId);
//Console.WriteLine("请输入审批意见(通过/拒绝):");
//var auditRemark = Console.ReadLine();
////告诉流程引擎 我审批
//await host.PublishEvent("MyEvent", workflowId, auditRemark);


//var workflowId = await host.StartWorkflow(nameof(MoreResultWorkflow), 1);
//Console.WriteLine("发起成功，流程实例ID：" + workflowId);


//var workflowId = await host.StartWorkflow(nameof(ForeachWorkflow), 1);
//Console.WriteLine("发起成功，流程实例ID：" + workflowId);

//var loopData = new LoopData() { LoopCount = 1 };
//var workflowId = await host.StartWorkflow(nameof(WhileWorkflow), 1, loopData);
//Console.WriteLine("发起成功，流程实例ID：" + workflowId);

//for (int i = 1; i <= 4; i++)
//{
//    Console.WriteLine("请输入第{0}个人的审批意见（通过/拒绝）", i);
//    var auditRemark = Console.ReadLine();
//    //告诉流程引擎 我审批
//    await host.PublishEvent("MyEvent", workflowId + i, auditRemark);
//}

var loopData = new LoopData() { LoopCount = 2 };
var workflowId = await host.StartWorkflow(nameof(IfWorkflow), 1, loopData);
Console.WriteLine("发起成功，流程实例ID：" + workflowId);


//阻塞程序执行，防止控制台窗口快速关闭
Console.ReadLine();

//流程结束后，停止工作流主机
host.Stop(); 

//配置服务（将配置服务放在一个方法中，方便调用）
static IServiceProvider ConfigureServices()
{
    IServiceCollection services = new ServiceCollection();
    services.AddLogging(); // WorkflowCore需要用到 logging service
    services.AddWorkflow();

    var serviceProvider = services.BuildServiceProvider();

    return serviceProvider;
}